﻿using System;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace AEM.Data.BLL.Users
{
    /// <summary>
    /// 用户登录成功后的初始化
    /// </summary>
    public class LoginSuccessInit : IDisposable
    {
        /// <summary>
        /// 初始化成功事件
        /// </summary>
        public event EventHandler InitSuccessEvent;
        private static UserLoginSuccessArgs loginArgs;

        static LoginSuccessInit()
        {
            Actions.User.init();
        }

        public LoginSuccessInit(UserLoginSuccessArgs loginArgs)
        {
            if (null == loginArgs) throw new Exception("用户登录失败！");

            LoginSuccessInit.loginArgs = loginArgs;

            Config.userLogin = loginArgs;
            //Config.dataBaseDataset = this.InitDataBase();

            //注册用户退出
            Actions.User.LoginOut = this.Dispose;
            Thread t = new Thread(() =>
            {
                if (null != InitSuccessEvent)
                    InitSuccessEvent.BeginInvoke(this, new EventArgs(), new AsyncCallback(Callback), null);
            }) { IsBackground = true };
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private void Callback(IAsyncResult tag)
        {
            AsyncResult result = (AsyncResult)tag;
            EventHandler del = (EventHandler)result.AsyncDelegate;
            del.EndInvoke(tag);
        }

        #region IDisposable 成员

        public void Dispose()
        {
            //Config.dataBaseDataset.Dispose();
        }

        #endregion
    }
}
